var i, shop_item, shop_item_total, shop_item_current, shop_menu_color, margin_x, margin_y;
margin_x = view_xview[0] + 16;
margin_y = view_yview[0] + 16;
if (trigger_get("shop") == true)
{
    shop_item_total = ds_list_size(global.shop_list);
    shop_item_current = 0;
    draw_set_color(c_black);
    draw_set_alpha(0.5);
    draw_rectangle(view_xview[0], view_yview[0], view_xview[0] + view_wview[0], view_yview[0] + view_hview[0], false);
    draw_set_alpha(1);
    textbox_draw_canvas(margin_x, margin_y, (view_wview[0] div 2) - 24, 16 + (min(16, shop_item_total) * 10));
    var shop_item_index, shop_item_price;
    for (shop_item = 0; shop_item < shop_item_total; shop_item++)
    {
        shop_item_index = ds_list_find_value(global.shop_list, shop_item);
        shop_item_price = ds_map_find_value(global.shop_items, shop_item_index);
        if (shop_item_transaction == false)
        {
            if (shop_item_selected == shop_item)
            {
                draw_set_color(c_menured1);
                draw_rectangle(margin_x + 5, margin_y + 7 +(shop_item * 10),view_xview[0] + (view_wview[0]/2) - 14,margin_y + 16 + (shop_item * 10),false);
                if (shop_item_price >= variable_get("money")) or (inventory_item_cancarry(shop_item_index, 1) == false)
                {
                    shop_draw_color = c_menuyellow1;
                }
                else
                {
                    shop_draw_color = c_white;
                }
            }
            else
            {
                if (shop_item_price >= variable_get("money")) or (inventory_item_cancarry(shop_item_index, 1) == false)
                {
                    shop_draw_color = c_menured1;
                }
                else
                {
                    shop_draw_color = c_menuyellow1;
                }
            }
        }
        else
        {
            shop_draw_color = c_gray;
        }
        draw_set_halign(fa_left);
        textbox_draw_text(margin_x + 8, margin_y + 8 +(shop_item * 10), ds_grid_get(global.item,0,shop_item_index), shop_draw_color, 0);
        draw_set_halign(fa_right);
        textbox_draw_text(view_xview[0] + (view_wview[0] div 2) - 16, margin_y + 8 + (shop_item * 10),"$" + string(shop_item_price), shop_draw_color, 0);
    }
    var shop_item_selected_index, shop_item_string, shop_inventory_current_string, shop_inventory_current_sprite;
    if (shop_item_transaction == true)
    {
        shop_draw_color = c_gray;
    }
    else
    {
        shop_draw_color = c_white;
    }
    shop_item_selected_index = ds_list_find_value(global.shop_list, shop_item_selected);
    shop_item_string = ds_grid_get(global.item, 2, shop_item_selected_index);
    shop_item_string_height = string_height_ext(shop_item_string, -1, 256) + 16;
    draw_set_halign(fa_left);
    textbox_draw_canvas(margin_x, view_yview[0] + (view_hview[0] - shop_item_string_height) - 16, 288, shop_item_string_height);
    textbox_draw_text_ext(margin_x + 8, view_yview[0] + (view_hview[0] - shop_item_string_height) - 8, shop_item_string, -1, 256, shop_draw_color, 0);
    //Inventory
    shop_inventory_current_string = string((global.team_member_current_id).character_name) + "'s Inventory";
    shop_inventory_current_sprite = (global.team_member_current_id).character_sprite_stand[0];
    textbox_draw_canvas(view_xview[0] + (view_wview[0] div 2), margin_y, 16 + (inventory_itemsrow * inventory_cellsize), 16 + (inventory_rows * inventory_cellsize));
    if (ds_list_size(global.team) > 1)
    {
        draw_set_halign(fa_right);
        textbox_draw_text(view_xview[0] + view_wview[0] - 24, margin_y + (inventory_rows * inventory_cellsize), shop_inventory_current_string, c_gray, 0);
        draw_sprite_ext(shop_inventory_current_sprite, 0, view_xview[0] + (view_wview[0] div 2) + 18, margin_y + (12 + (inventory_rows * inventory_cellsize)) - sprite_get_height(shop_inventory_current_sprite), 1, 1, 0, c_white, 0.5);
    }
    for (i = 0; i < global.inventory_current_maxitems; i++)
    {
        inventory_draw_item(i, (view_wview[0] div 2) + 8, 24);
    }
    inventory_draw_currency(view_xview[0] + 8 + (view_wview[0] div 2), view_yview[0] + 8, string("$"));
    //Menu
    if (shop_item_transaction == true)
    {
        var shop_transaction_string, shop_transaction_string_width;
        if (variable_get("money") >= (shop_item_transaction_price * shop_item_transaction_quantity)) && (inventory_item_cancarry(shop_item_transaction_index,shop_item_transaction_quantity) == true)
        {
            shop_draw_color = c_menuyellow1;
        }
        else
        {
            shop_draw_color = c_menured1;
        }
        shop_transaction_string[0] = string(ds_grid_get(global.item,0,shop_item_transaction_index))+ " x" + string(shop_item_transaction_quantity);
        shop_transaction_string[1] = "$" + string(shop_item_transaction_price * shop_item_transaction_quantity);
        shop_transaction_string_width = max(string_width(shop_transaction_string[0]) + string_width(shop_transaction_string) + 64, 128);
        textbox_draw_canvas(view_xview[0] + (view_wview[0]/2) - (shop_transaction_string_width div 2), view_yview[0] + (view_hview[0] div 2) - 23, shop_transaction_string_width, 46);
        draw_set_halign(fa_left);
        textbox_draw_text(view_xview[0]+(view_wview[0]/2) - (shop_transaction_string_width div 2) + 8, view_yview[0] + (view_hview[0]/2) - 15, shop_transaction_string[0], shop_draw_color, 0);
        draw_set_halign(fa_right);
        textbox_draw_text(view_xview[0] + (view_wview[0]/2) + (shop_transaction_string_width div 2) - 8, view_yview[0] + (view_hview[0] div 2) - 15, shop_transaction_string[1], shop_draw_color, 0);
        draw_set_halign(fa_center);
        for (shop_transaction_option = 0; shop_transaction_option < 2; shop_transaction_option++)
        {
            if (shop_transaction_option == shop_item_transaction_selected)
            {
                draw_set_color(c_menured1);
                draw_rectangle(view_xview[0] + (view_wview[0]/2) - (shop_transaction_string_width div 2) + 5, view_yview[0] + (view_hview[0]/2) - 6 + (shop_transaction_option * 10),view_xview[0] + (view_wview[0] div 2) + (shop_transaction_string_width div 2) - 6,view_yview[0] + (view_hview[0]/2) + 3 + (shop_transaction_option * 10),false);
                shop_draw_color = c_white;
            }
            else
            {
                shop_draw_color = c_menuyellow1;
            }
            switch (shop_transaction_option)
            {
                case 0:
                textbox_draw_text(view_xview[0] + (view_wview[0] div 2) ,view_yview[0] + (view_hview[0]/2) - 5 + (shop_transaction_option * 10), "Buy Item", shop_draw_color, 0);
                break;
                case 1:
                textbox_draw_text(view_xview[0] + (view_wview[0] div 2), view_yview[0] + (view_hview[0]/2) - 5 + (shop_transaction_option * 10), "Cancel", shop_draw_color, 0);
                break;
            }
        }
        draw_set_halign(fa_left);
    }
}
